<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Common\Controller\AdminController;

/**
 * 信息管理
 */
class MessageController extends AdminController{
	
	
	/**
	 * 短信设置
	 */
    public function Tel(){
		
		if(IS_AJAX){
			
		
			$m_message_conf = M('message_conf');
			
			$map['id'] = 2;
			
			
			$data = I('');
			
			
			$result = $m_message_conf->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
	
			$this->ajaxReturn($info);
		
		}else{
			
			
			//页面配置
			$page = array(
				
				'title'=>'短信设置',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
		
			$m_message_conf = M('message_conf');
			
			$map['id'] = 2; //约定 id=1是微信
			
			$data = $m_message_conf->where($map)->find();
			
			$this->assign('data',$data);

			$base_url = $_SERVER['SERVER_NAME'];
		
			$this->assign('base_url',$base_url);
			
		
		
		
			$this->display();
		}
		
	}
	
	/**
	 * 发送短信
	 */
    public function SendTel(){
		
		if(IS_POST){
			

			$tel = I('param.tel');
			$value = I('param.value','','');
			
			//发送短信
			smsbao_send($tel,$value);
			
			$m_message_log = M('message_log');
			
			$data['account'] = $tel;
			$data['value'] = $value;
			
			$data['type'] = 1;
			$data['time'] = time();
			$data['ip'] =get_client_ip();
			$data['uid'] =getUID();
			$data['status'] =1;
			$m_message_log->add($data);
			
			//TODO返回值
			$this->success('发送成功');
			
		}else{
			
			
			//页面配置
			$page = array(
				
				'title'=>'发送邮件',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
			
			$this->display();
		}
		
	}
	
	/**
	 * 邮件配置
	 */
    public function EMail(){
		
		if(IS_AJAX){
			
		
			$m_message_conf = M('message_conf');
			
			$map['id'] = 1;
			
			
			$data = I('');
			
			
			$result = $m_message_conf->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
	
			$this->ajaxReturn($info);
		
		}else{
			
			
			//页面配置
			$page = array(
				
				'title'=>'邮箱设置',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
			
			$m_message_conf = M('message_conf');
			
			$map['id'] = 1; //约定 id=1是邮件
			
			$data = $m_message_conf->where($map)->find();
			
			$this->assign('data',$data);

			$base_url = $_SERVER['SERVER_NAME'];
		
			$this->assign('base_url',$base_url);
			
		
		
		
			$this->display();
		}
		
	}
	
	/**
	 * 发送邮件
	 */
    public function SendEMail(){
		
		if(IS_POST){
			
		
			$mails = new \Think\Mail();
				
			//$mail->SendMail('n@isdce.com','标题','内容','发件人');
			
			$name = I('param.name');
			$title = I('param.title');
			$value = I('param.value','','');
			$mail = I('param.mail');
			
			$mails->SendMail($mail,$title,$value,$name);
			
			$m_message_log = M('message_log');
			
			$data['account'] = $mail;
			$data['value'] = $value;
			$data['name'] = $name;
			$data['title'] = $title;
			$data['type'] = 2;
			$data['time'] = time();
			$data['ip'] =get_client_ip();
			$data['uid'] =getUID();
			$data['status'] =1;
			$m_message_log->add($data);
			
			$this->success('发送成功');
			
		}else{

			//页面配置
			$page = array(
				
				'title'=>'发送邮件',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
			
			$this->display();
		}
		
	}
	/**
	 * 行为记录
	 */
    public function Manage(){
		
		action_log('admin_behavior_log');//行为记录
		

		//页面配置
		$page = array(
			
			'title'=>'发送记录',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
			
		//实例化数据库
		$m_message_log = M('message_log');
		
		
		
		
		//获得查询用户UID
		$type = I('param.type')?I('param.type'):'';

		if($type!=0){
			
			$map['type'] =$type;
		}
		
		
		//开始和结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		
		
		//默认第一页
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		//分页查询
		$list = $m_message_log->where($map)->order('time desc')->page($_GET['p'].',50')->select();
		$count      = $m_message_log->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		
		
		
		$this->assign('list',$list);
		
		
		$this->display();
		
	}
	
	
	
}